package com.autoglm.ui.screens

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.autoglm.ui.components.VirtualAssistant
import com.autoglm.ui.components.VoiceButton
import com.autoglm.ui.components.TaskCard
import com.autoglm.ui.theme.*
import com.autoglm.ui.viewmodel.MainViewModel

/**
 * 主屏幕
 * AutoGLM应用的主界面
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MainScreen(
    modifier: Modifier = Modifier,
    viewModel: MainViewModel = hiltViewModel()
) {
    val uiState by viewModel.uiState.collectAsState()
    
    Box(
        modifier = modifier
            .fillMaxSize()
            .background(
                brush = Brush.verticalGradient(
                    colors = listOf(
                        GradientBackgroundStart,
                        GradientBackgroundEnd
                    )
                )
            )
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // 顶部标题
            Text(
                text = "AutoGLM",
                style = MaterialTheme.typography.headlineLarge,
                color = Accent500,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(top = 32.dp, bottom = 16.dp)
            )
            
            // 虚拟助手
            VirtualAssistant(
                modifier = Modifier
                    .size(120.dp)
                    .padding(16.dp),
                isListening = uiState.isListening,
                isProcessing = uiState.isProcessing,
                onClick = { viewModel.onAssistantClick() }
            )
            
            // 状态文本
            Text(
                text = when {
                    uiState.isListening -> "正在听取指令..."
                    uiState.isProcessing -> "正在处理任务..."
                    uiState.currentTask != null -> "任务执行中..."
                    else -> "说出你的指令"
                },
                style = MaterialTheme.typography.bodyLarge,
                color = Neutral300,
                modifier = Modifier.padding(16.dp)
            )
            
            // 任务列表
            if (uiState.recentTasks.isNotEmpty()) {
                Text(
                    text = "最近任务",
                    style = MaterialTheme.typography.titleMedium,
                    color = Accent400,
                    fontWeight = FontWeight.SemiBold,
                    modifier = Modifier
                        .padding(16.dp)
                        .align(Alignment.Start)
                )
                
                LazyColumn(
                    modifier = Modifier.weight(1f),
                    verticalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    items(uiState.recentTasks) { task ->
                        TaskCard(
                            task = task,
                            onClick = { viewModel.onTaskClick(task.id) }
                        )
                    }
                }
            } else {
                Spacer(modifier = Modifier.weight(1f))
            }
            
            // 语音按钮
            VoiceButton(
                modifier = Modifier
                    .size(80.dp)
                    .padding(16.dp),
                isListening = uiState.isListening,
                isProcessing = uiState.isProcessing,
                onClick = { viewModel.onVoiceButtonClick() }
            )
        }
        
        // 错误消息
        uiState.errorMessage?.let { error ->
            LaunchedEffect(error) {
                // 显示错误消息
                viewModel.clearError()
            }
        }
    }
}
